package com.colodoo.framework.manager.menu.service;

import com.colodoo.framework.base.abs.BaseService;
import com.colodoo.framework.easyui.Page;
import com.colodoo.framework.easyui.Tree;
import com.colodoo.framework.manager.menu.model.Menu;
import com.colodoo.framework.manager.menu.model.MenuVO;
import com.colodoo.framework.manager.menu.service.mapper.MenuRepository;
import com.colodoo.framework.manager.menu.service.mapper.MenuSQLMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author colodoo
 * @date 2019-4-24 22:32:52
 * @description
 */
@Service
@Slf4j
public class MenuService extends BaseService<Menu> {

    @Resource
    private MenuSQLMapper sqlMapper;
    @Resource
    private MenuRepository repository;

    /**
     * 新增数据
     *
     * @param model
     * @return
     */
    public void saveMenu(Menu model) {
        this.repository.save(model);
    }

    /**
     * 删除数据
     *
     * @param model
     * @return
     */
    public void deleteMenu(Menu model) {
        this.repository.delete(model);
    }

    /**
     * 更新数据
     *
     * @param model
     * @return
     */
    public void updateMenu(Menu model) {
        this.repository.save(model);
    }

    /**
     * 根据id查找单条数据
     *
     * @param model
     * @return
     */
    public Menu queryById(Menu model) {
        return this.repository.getOne(model.getMenuId());
    }

    /**
     * 查找列表
     *
     * @return
     */
    public List<Menu> query(MenuVO model) {
        return sqlMapper.getMenuList(model);
    }

    /**
     * 查找分页列表
     *
     * @param page
     * @return
     */
    public PageInfo<Menu> query(Page page, MenuVO model) {
        PageInfo<Menu> pageInfo;
        PageHelper.startPage(page.getPage(), page.getRows());
        List<Menu> list = sqlMapper.getMenuList(model);
        pageInfo = new PageInfo<Menu>(list);
        return pageInfo;
    }

    public List<Tree> getTreeMenu(String parentMenuId) {
        if (parentMenuId == null) {
            parentMenuId = "";
        }
        List<Tree> list = new ArrayList<Tree>();
        List<Menu> menus = this.repository.findAll(
                Example.of(Menu.builder().parentMenuId(parentMenuId).build()),
                Sort.by("sort")
        );
        for (Menu menu : menus) {
            Tree tree = Tree.builder()
                    .id(menu.getMenuId())
                    .text(menu.getMenuName())
                    .title(menu.getMenuName())
                    .label(menu.getMenuName())
                    .state("close")
                    .build();
            tree.setAttributes(menu);
            List tempList = getTreeMenu(tree.getId());
            if (tempList.size() != 0) {
                tree.setChildren(tempList);
            }
            list.add(tree);
        }
        return list;
    }
}
